from myredis import r
import time
from utlis.mysql import db
def deletetoken():
    # 获取token
    nowtime= int(time.time())-360
    key="logoutlist"
    res=r.sortedset_getbyscore(key,0,nowtime)
    for i in res:
        r.sortedset_delete(key,i.decode())
    print("定时任务执行中……哒哒哒")

def delpassdata():
    nowtime = int(time.time())-1800
    lst = r.sortedset_getbyscore('yanshiduilie',0,int(nowtime))
    print('lst>>>>>',lst)
    if lst:
        for i in lst:
            r.sortedset_delete('yanshiduilie',i.decode())
            sql = "delete from order_form where ddorder='%s'"%(i.decode())
            db.add(sql)
            db.commit()
        print('结束')
    else:
        print('没数据了')
# delpassdata()
import datetime,time
# 删除七天没满标的申请
def delweiman():
    new_time = datetime.datetime.now() - datetime.timedelta(days=1)
    now_time = datetime.datetime.strftime(new_time, '%Y-%m-%d %H:%M:%S')
    print(now_time)
    sql = """select code from landmarks where audit_time < '%s' and status=3  union all select code from landmarks1 where audit_time < '%s' and status=3
     union all select code from landmarks2 where audit_time < '%s'and status=3""" %(now_time,now_time,now_time)
    res = db.findAll(sql)
    print('res>>>>',res)
# delweiman()
import random
def tender_land():
    key = 'bid'
    new_time = time.time() - 20
    bid = r.sortedset_getbyscore(key,0,new_time)

    for i in bid:
        print(i)
        number = hash(i.decode()) % 3
        if number != 0:
            sql = """select * from landmarks%d where code=%d"""%(int(number),int(i.decode()))
            res = db.findone(sql)
            print(res)
            if int(res['money']) == 0:
                sqls = """update landmarks%d set status=2 where code=%d"""%(int(number),int(i.decode()))
                db.add(sqls)
                sqlss = """update invest set status=1 where bdid=%d"""%(int(i.decode()))
                db.add(sqlss)
                db.commit()
                r.sortedset_delete(key,i.decode())
                print('满标成功')
            else:
                sqls = """update landmarks%d set status=3 where code=%d""" % (int(number), int(i.decode()))
                db.add(sqls)
                sqlss = """update invest set status=2 where bdid=%d""" % (int(i.decode()))
                db.add(sqlss)
                db.commit()
                r.sortedset_delete(key, i.decode())
                print('投标失败')
        else:
            sql = """select * from landmarks where code=%d""" % (int(i.decode()))
            res = db.findone(sql)
            print(res)
            if int(res['money']) == 0:
                sqls = """update landmarks set status=2 where code=%d""" % (int(i.decode()))
                db.add(sqls)
                sqlss = """update invest set status=1 where bdid=%d""" % (int(i.decode()))
                db.add(sqlss)
                db.commit()
                r.sortedset_delete(key,i.decode())
                print('满标成功')
            else:
                sqls = """update landmarks set status=3 where code=%d""" % (int(i.decode()))
                db.add(sqls)
                sqlss = """update invest set status=2 where bdid=%d""" % (int(i.decode()))
                db.add(sqlss)
                db.commit()
                r.sortedset_delete(key, i.decode())
                print('投标失败')
    a = random.randint(1,10)
    print('满标查询程序运行中',a)
# tender_land()

# es同步数据
from elasticsearch import Elasticsearch
es = Elasticsearch("http://47.104.108.172:9200/")
def getes():
    maxcode=r.get_str("maxcode1")
    if maxcode:
        maxcode=maxcode.decode()
        sql=f"""select * from landmarks where code>{maxcode} union all select * from landmarks1  where code>{maxcode} union all select * from landmarks2  where code>{maxcode};"""
        print("sql>>>>",sql)
        res=db.findAll(sql)
        list = []
        for i in res:
            list.append(i['code'])
            es.index(index='biao', doc_type="1", body={
                'code': i['code'],
                'descrip': i['descrip'],
                'tmoney': i['tmoney'],
                'interest_rate': i['interest_rate'],
                'term': i['term'],
                'status': i['status'],
                'audit_userid': i['audit_userid'],
                'money': i['money'],
            })
    else:
        sql="""select * from landmarks union all select * from landmarks1 union all select * from landmarks2"""
        res=db.findAll(sql)
        list = []
        for i in res:
            list.append(i['code'])
            es.index(index='biao',doc_type="1",body={
                'code': i['code'],
                'descrip': i['descrip'],
                'tmoney': i['tmoney'],
                'interest_rate': i['interest_rate'],
                'term': i['term'],
                'status': i['status'],
                'audit_userid': i['audit_userid'],
                'money': i['money'],
            })
        mexcode=list[-1:][0]
        print("list>>>",mexcode)
        r.set_str("maxcode1",mexcode)
# getes()
import datetime
import redis
ra= redis.Redis(host='localhost', port=6379, db=2)
def add_to_cart(user_id, product_id, quantity):
    r.hset(user_id, product_id, quantity)















